package com.arrogames.rgblabyrinth.game.trigger.effect {
	import com.arrogames.lib.ash.animation.Animation;
	import com.arrogames.lib.ash.animation.IAnimatable;
	import com.arrogames.rgblabyrinth.game.EntityCreator;
	import com.arrogames.rgblabyrinth.game.lighting.Vision;
	
	import ash.core.Entity;
	
	public class BlinkEffect implements IEffect, IAnimatable {

		private var runningFor:Number = 0;
		private var toggleAt:Array = [
			0.010,
			0.736,
			0.968,
			2.126,
			2.843,
			3.096,
			3.533,
			4.087
		];
		private var currentBlinkIndex:int = 0;
		private var message:String;
		
		private var isOn:Boolean = false;

		private var creator:EntityCreator;
		private var entity:Entity;
		private var vision:Vision;
		
		public function BlinkEffect(message:String, entity:Entity, vision:Vision) {
			this.vision = vision;
			this.entity = entity;
			this.message = message;
			isOn = true;
		}
		
		public function execute(creator:EntityCreator):void {
			this.creator = creator;
			entity.add(new Animation(this));
		}
		
		public function update(time:Number):void {
			if (toggleAt[currentBlinkIndex] < runningFor) {
				currentBlinkIndex++;
				
				isOn = !isOn;
				if (isOn) {
					vision.brightness = 1;
				} else {
					vision.brightness = 0;
				}
				
				if (currentBlinkIndex == toggleAt.length) {
					vision.brightness = 0;
					creator.destroyEntity(entity);
				}
			}
			runningFor = runningFor+ time;
		}
		
	}
}